-- reward_knowledge
-- Created by panyl
-- 成就：奖励工程学手册知识

-- 内部函数声明
local whenRewardKnowledge;

-- 注册圣器升级的回调
EventMgr.register("achievements/reward_knowledge", event.REWARD_ENGINEERING_KNOWLEDGE, function(args)
    whenRewardKnowledge(args);
end);

-- 学习工程学手册的回调
whenRewardKnowledge = function(args)
    -- 玩家不在迷宫中
    if not DungeonM.isInDungeon() then
        return;
    end

    -- 获取当前所在的迷宫编号
    local dungeonId = DungeonM.getDungeonId();

    -- 遍历所有的子规则
    local ids = AchievementM.getIdsByRule("reward_knowledge");

    -- 判断是否是符合要求的成就
    local function isValidId(id)
        if true == AchievementM.isCompleted(id) then
            -- 已经完成了
            return false;
        end

        if not AchievementM.isBelongTo(id, dungeonId) then
            -- 不是该迷宫的成就
            return false;
        end

        local config = AchievementM.query(id);
        local from = config["rule_arg"]["from"];

        -- 没有配置来源
        if not from then
            return false;
        end

        local extra = args.extra;
        if type(extra) ~= "table" or extra.from ~= from then
            return false;
        end

        return true;
    end

    for _, id in pairs(ids) do
        if isValidId(id) then
            -- 尝试完成成就
            AchievementM.tryComplete(id, 1);
        end
    end
end

return {

};
